package com.lgj.demo0812.TestDemo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.lgj.demo0812.pojo.User;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lizy51
 * @date 2022-09-13 19:55
 * @mark:SpringMvc版本不高的话，Jasckon版本不能太高
 */
public class FormatDemo {
    public static void main(String[] args) throws Exception {
//        System.out.println(incrPrimaryKey("7"));
        System.out.println(jasonConvertObject());
        System.out.println(objConvertjson());
    }

    public static String incrPrimaryKey(String length) throws Exception{
        Long incr = 111L;
        //length 决定 字符串多少位 ,
        //case : String.format("%04d",99")  = 0099
        //表示一共四位，最后是99，剩余的前面补0
        //https://cloud.tencent.com/developer/article/1607231
        String format = String.format("%0" + length + "d", incr);
        return format;
    }

    /**
     *
     * @author lizy51
     * @date 2022-09-14 15:04
     * @return java.lang.String
     * @des:Json字符串转换成实体类
     **/
    public static String jasonConvertObject() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        String text = null;
        text = "{\"userName\":\"AAA\",\"passWord\":\"123\",\"remark\":null,\"treeList\":[\"唱歌\",\"篮球\"]}";
        User user = objectMapper.readValue(text, User.class);
        return user.toString();
    }

    /**
     *
     * @author lizy51
     * @date 2022-09-14 15:54
     * @return java.lang.String
     * @des:Java对象转化成Json
     **/
    public static String objConvertjson() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        List<String> list = new ArrayList<String>();
        list.add("唱歌");
        list.add("篮球");
        return objectMapper.writeValueAsString(new User(null,"AAA", "123",null,list));
    }


}
